home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / graphics / gnuplot / contrib / hanna / gnulib_x11.c < prev    next >
C/C++ Source or Header  |  1992-04-16  |  9KB  |  333 lines

  1. #ifndef lint
  2. static char *RCSid = "$Id: gnulib_x11.c,v 1.00 1992/03/31 18:03:00 gregor Exp gregor $";
  3. #endif
  4.  
  5. /*
  6.  * $Log: gnulib_x11.c,v $
  7.  * Revision 1.00  1992/03/31  18:03:00  gregor
  8.  * gnuplot3.2, beta 6
  9.  *
  10.  */
  11.  
  12. /*-----------------------------------------------------------------------------
  13.  *   gnulib_x11 - linkable X11 outboard terminal driver for gnuplot 3
  14.  *
  15.  *   Based on gnuplot_x11.c by Ed Kubaitis (University of Illinois)
  16.  *
  17.  *   This code is provided as is and with no warranties of any kind.
  18.  *       
  19.  *   gregg hanna (gregor@kafka.saic.com)
  20.  *   Science Applications International Corporation
  21.  *---------------------------------------------------------------------------*/
  22.  
  23. #include "gnulib_x11.h"
  24.  
  25. #define Ncolors 13
  26. static Pixel *colors;
  27. static XFontStruct *font;
  28. static Display *dpy;
  29. static Drawable pixmap;
  30.  
  31. static char dashes[10][5] = { {0}, {1,6,0}, 
  32.    {0}, {4,2,0}, {1,3,0}, {4,4,0}, {1,5,0}, {4,4,4,1,0}, {4,2,0}, {1,3,0}
  33.    };
  34.  
  35. static int cx=0, cy=0, vchar;
  36. static enum JUSTIFY { LEFT, CENTRE, RIGHT } jmode;
  37.  
  38.  
  39. /*-----------------------------------------------------------------------------
  40.  *   GP_PlotFromStream - reads a gnuplot (xlib) from an open stream
  41.  *---------------------------------------------------------------------------*/
  42.  
  43. int GP_PlotFromStream(fp,disp,pmap,colortable,ufont)
  44.      FILE *fp;
  45.      Display *disp;
  46.      Drawable pmap;
  47.      Pixel *colortable;
  48.      XFontStruct *ufont;
  49. {
  50.   int i;
  51.   static int accept();
  52.  
  53.   if ( colortable == NULL ) return -1;
  54.   colors = colortable;
  55.   font = ufont;
  56.   dpy = disp;
  57.   pixmap = pmap;
  58.   while (!(i=accept(fp)));
  59.   return i;
  60. }
  61.  
  62. /*-----------------------------------------------------------------------------
  63.  *   GP_PlotFromFile - reads a gnuplot from a file, drawing it in a pixmap
  64.  *---------------------------------------------------------------------------*/
  65.  
  66. int GP_PlotFromFile(fn,disp,pmap,colortable,ufont)
  67.      char *fn;
  68.      Display *disp;
  69.      Drawable pmap;
  70.      Pixel *colortable;
  71.      XFontStruct *ufont;
  72. {
  73.   FILE *fp;
  74.   int i;
  75.  
  76.   fp = fopen(fn,"r");
  77.   if ( fp == NULL ) return -1;
  78.   i = GP_PlotFromStream(fp,disp,pmap,colortable,ufont);
  79.   fclose(fp);
  80.   return i;
  81. }
  82.  
  83. /*-----------------------------------------------------------------------------
  84.  *   accept - accept & record new plot from gnuplot inboard X11 driver
  85.  *---------------------------------------------------------------------------*/
  86.  
  87. #define NBUF 1024
  88. static int nc = 0;
  89. static int ncalloc = 0;
  90. static char **commands = (char**)0;
  91.  
  92. static int accept(f)
  93.      FILE *f;
  94. {
  95.   char buf[NBUF];
  96.   /* 
  97.   static int display();
  98.   */
  99.   int display();
  100.  
  101.    while (fgets(buf, NBUF, f)) {
  102.       if (*buf == 'G') {                           /* enter graphics mode */
  103.      if (commands) {
  104.         int n; for (n=0; n<nc; n++) free(commands[n]);
  105.         free(commands);
  106.         }
  107.      commands = (char **)0; nc = ncalloc = 0;
  108.          }
  109.       else if (*buf == 'E') {                      /* leave graphics mode */
  110.     display();
  111.     if (commands) {
  112.       int n; for (n=0; n<nc; n++) free(commands[n]);
  113.       free(commands);
  114.     }
  115.     commands = (char **)0; nc = ncalloc = 0;
  116.     return 0;
  117.       }
  118.       else if (*buf == 'R') { return 1; }         /* leave X11/x11 mode  */
  119.       else {                                      /* record command      */
  120.      char *p;
  121.      if (nc >= ncalloc) {
  122.         ncalloc = ncalloc*2 + 1;
  123.         commands = (commands)
  124.            ? (char **)realloc(commands, ncalloc * sizeof(char *))
  125.            : (char **)malloc(sizeof(char *));
  126.         }
  127.      p = (char *)malloc((unsigned)strlen(buf)+1);
  128.      if (!commands || !p) return -1;
  129.      commands[nc++] = strcpy(p, buf);
  130.      }
  131.       }
  132.    }
  133.  
  134. /*-----------------------------------------------------------------------------
  135.  *   display - display last plot from gnuplot inboard X11 driver
  136.  *---------------------------------------------------------------------------*/
  137.  
  138. #define X(x) (int) (x * xscale)
  139. #define Y(y) (int) ((4095-y) * yscale)
  140. int display()
  141. {
  142.    int n, x, y, sw, sl, lt, width, type, W, H;
  143.    char *buf, *str;
  144.    double xscale, yscale;
  145.    Window root;
  146.    unsigned int d0;
  147.    int d1, myfont;
  148.    GC gc;
  149.  
  150.    XGetGeometry(dpy,pixmap,&root,&d0,&d0,&W,&H,&d1,&d1);
  151.  
  152.    if (!nc) return;
  153.  
  154.    /* set scaling factor between internal driver & window geometry */
  155.    xscale = (double)W / 4096.;  yscale = (double)H / 4096.;  
  156.  
  157.    gc = XCreateGC(dpy,pixmap,0,(XGCValues*)NULL);
  158.  
  159.    /* set font parameters */
  160.  
  161.    myfont = 0;
  162.    if ( font == NULL )  {
  163.     font = XLoadQueryFont(dpy,"9x15");
  164.     if ( font != NULL ) myfont = 1;
  165.    }
  166.    if ( font != NULL ) XSetFont(dpy,gc,font->fid);
  167.  
  168.    if ( font == NULL ) vchar = 30;
  169.    else vchar = font->ascent + font->descent;
  170.  
  171.    /* set pixmap background */
  172.    XSetForeground(dpy, gc, colors[0]);
  173.    XSetBackground(dpy, gc, colors[0]);
  174.  
  175.    /* loop over accumulated commands from inboard driver */
  176.    for (n=0; n<nc; n++) {
  177.       buf = commands[n];
  178.  
  179.       /*   X11_vector(x,y) - draw vector  */
  180.       if (*buf == 'V') { 
  181.      sscanf(buf, "V%4d%4d", &x, &y);  
  182.      XDrawLine(dpy, pixmap, gc, X(cx), Y(cy), X(x), Y(y));
  183.      cx = x; cy = y;
  184.      }
  185.  
  186.       /*   X11_move(x,y) - move  */
  187.       else if (*buf == 'M') 
  188.      sscanf(buf, "M%4d%4d", &cx, &cy);  
  189.  
  190.       /*   X11_put_text(x,y,str) - draw text   */
  191.       else if (*buf == 'T') { 
  192.      sscanf(buf, "T%4d%4d", &x, &y);  
  193.      str = buf + 9; sl = strlen(str) - 1;
  194.      sw = 0;
  195.      if ( font != NULL ) {
  196.        sw = XTextWidth(font, str, sl);
  197.        switch(jmode) {
  198.        case LEFT:   sw = 0;     break;
  199.        case CENTRE: sw = -sw/2; break;
  200.        case RIGHT:  sw = -sw;   break;
  201.        }
  202.      }
  203.      XSetForeground(dpy, gc, colors[2]);
  204.      XDrawString(dpy, pixmap, gc, X(x)+sw, Y(y)+vchar/3, str, sl);
  205.      XSetForeground(dpy, gc, colors[lt+3]);
  206.      }
  207.  
  208.       /*   X11_justify_text(mode) - set text justification mode  */
  209.       else if (*buf == 'J') 
  210.      sscanf(buf, "J%4d", &jmode);
  211.  
  212.       /*   X11_linetype(type) - set line type  */
  213.       else if (*buf == 'L') { 
  214.      sscanf(buf, "L%4d", <);
  215.      lt = (lt%8)+2;
  216.      width = (lt == 0) ? 2 : 0;
  217.      if (colors[Ncolors] == (Pixel)0) {
  218.        /* not monochrome */
  219.         if (lt != 1) 
  220.            type = LineSolid;
  221.         else {
  222.            type = LineOnOffDash;
  223.            XSetDashes(dpy, gc, 0, dashes[lt], strlen(dashes[lt]));
  224.            }
  225.         XSetForeground(dpy, gc, colors[lt+3]);
  226.         }
  227.      else {
  228.        /* monochrome */
  229.         type  = (lt == 0 || lt == 2) ? LineSolid : LineOnOffDash;
  230.         if (dashes[lt][0])
  231.            XSetDashes(dpy, gc, 0, dashes[lt], strlen(dashes[lt]));
  232.         }
  233.      XSetLineAttributes( dpy,gc, width, type, CapButt, JoinBevel);
  234.      }
  235.       }
  236.     XFreeGC(dpy,gc);
  237.     if (myfont) XFreeFont(dpy,font);
  238.  
  239.    }
  240.  
  241. /*-----------------------------------------------------------------------------
  242.  *   pr_color - determine color values
  243.  *---------------------------------------------------------------------------*/
  244.  
  245. Pixel *GP_ColorArray(bg,fg,c0,c1,c2,c3,c4,c5,c6,c7,c8)
  246.      Pixel bg,fg,c0,c1,c2,c3,c4,c5,c6,c7,c8;
  247. {
  248.   static Pixel colortable[Ncolors+1];
  249.   colors[0] = bg; /* background */
  250.   colors[1] = fg; /* bordercolor */
  251.   colors[2] = fg; /* text */
  252.   colors[3] = fg; /* border */
  253.   colors[4] = fg; /* axis */
  254.   colors[5] = c1;
  255.   colors[6] = c2;
  256.   colors[7] = c3;
  257.   colors[8] = c4;
  258.   colors[9] = c5;
  259.   colors[10] = c6;
  260.   colors[11] = c7;
  261.   colors[12] = c8;
  262.   colors[Ncolors] = (Pixel)0; /* not mono */
  263.   return colortable;
  264. }
  265.  
  266. static char color_values[Ncolors][30] = { 
  267.    "white", "black",  "black",  "black",  "black", 
  268.    "red",   "green",  "blue",   "magenta", 
  269.    "cyan",  "sienna", "orange", "coral" 
  270.    };
  271.  
  272. static char gray_values[Ncolors][30] = { 
  273.    "black",   "white",  "white",  "gray50", "gray50",
  274.    "gray100", "gray60", "gray80", "gray40", 
  275.    "gray90",  "gray50", "gray70", "gray30" 
  276.    };
  277.  
  278. Pixel *GP_AllocColor(dpy,mode)
  279.      Display *dpy;
  280.      int mode;
  281. {
  282.    Pixel black, white;
  283.    XColor used, exact;
  284.    Colormap cmap;
  285.    int n, Gray, Mono, Rv;
  286.    Pixel *colortable;
  287.    Visual *vis;
  288.    int scr;
  289.    char *v;
  290.  
  291.    colortable = (Pixel*)calloc(sizeof(Pixel),Ncolors+1);
  292.    if (!colortable) return NULL;
  293.  
  294.    scr = DefaultScreen(dpy);
  295.    black = BlackPixel(dpy,scr);
  296.    white = WhitePixel(dpy,scr);
  297.  
  298.    switch (mode) {
  299.    case 0: /* figure it out */
  300.      Gray = Mono = Rv = 0;
  301.      vis = DefaultVisual(dpy,scr);
  302.      if ( vis->class == GrayScale || vis->class == StaticGray )
  303.        if ( DefaultDepth(dpy,scr) == 1 ) Mono = 1;
  304.        else Gray = 1;
  305.      break;
  306.    case 1: Gray = 0; Mono = 0; Rv = 0; break;
  307.    case 2: Gray = 1; Mono = 0; Rv = 0; break;
  308.    case 3: Gray = 0; Mono = 1; Rv = 0; break;
  309.    default: Gray = 0; Mono = 1; Rv = 1; break;
  310.    }
  311.  
  312.    if (!Mono) {
  313.       cmap = DefaultColormap(dpy, scr);
  314.       for (n=0; n<Ncolors; n++) {
  315.      v = ((Gray) ? gray_values[n] : color_values[n]);
  316.      if (XAllocNamedColor(dpy, cmap, v, &used, &exact))
  317.         colortable[n] = used.pixel;
  318.      else {
  319.        /* fallback to monochrome */
  320.         Mono++;
  321.         break;
  322.         }
  323.      }
  324.       }
  325.    if (Mono) {
  326.       colortable[0] = (Rv) ? black : white ;
  327.       for (n=1; n<Ncolors; n++)  colortable[n] = (Rv) ? white : black;
  328.       }
  329.    if ( Mono ) colortable[Ncolors] = (Pixel)1;
  330.    return colortable;
  331.  }
  332.  
  333.